package cn.com.dhcc.turbo.service.stat;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.service.BaseService;
import cn.com.dhcc.app.pub.core.util.fusioncharts.Categories;
import cn.com.dhcc.app.pub.core.util.fusioncharts.Category;
import cn.com.dhcc.app.pub.core.util.fusioncharts.Chart;
import cn.com.dhcc.app.pub.core.util.fusioncharts.ChartDataset;
import cn.com.dhcc.app.pub.core.util.fusioncharts.ChartSet;
import cn.com.dhcc.app.pub.core.util.fusioncharts.ChartXmlUtil;
import cn.com.dhcc.turbo.dao.stat.FreqStatDao;
import cn.com.dhcc.turbo.entity.stat.FreqStat;

@Service
public class FreqStatService extends BaseService {
	@Autowired
	private FreqStatDao dao;
	final int SHOWNUM = 10; //TOP10，限制显示的节点或系统数量
	
	/**
	 * 节点间数据交换统计
	 * @param condition 未使用
	 * @param id 节点或系统id
	 * @param time 查询的时间条件
	 * @param timeType 年 或月
	 * @param level 节点 或系统
	 * @return
	 * @throws ServiceException
	 */
     public String getFreqStat(String id, String time, String timeType, String level)throws ServiceException {
           String msg = "";
           try {
        	   List< FreqStat> freqStatList = null;
        	   if(level.equals("node")){
        		   freqStatList = dao.getNodeFreqStat(id, time, timeType);
        	   }else{
        		   freqStatList = dao.getSysFreqStat(id, time, timeType);
        	   }
               
                Chart chart = new Chart();
                String extProperty = "labelDisplay='Rotate' slantLabels='1' rotateNames='0' plotSpacePercent='60' bgcolor='FFFFFF' plotgradientcolor=''"
                           +"showalternatehgridcolor='0' showplotborder='0' divlinecolor='CCCCCC'"
                           +"showvalues='0' showcanvasborder='0' labeldisplay='STAGGER' slantlabels='1'"
                           +"canvasborderalpha='0' legendshadow='0' legendborderalpha='0'"
                           +"showborder='0' formatNumberScale='0'";
                chart.setExtProperty(extProperty);
               
                List< Category> cList = new ArrayList <Category >();
                Categories categories = new Categories();
                int count = 0;
                for(Iterator<FreqStat> iterator = freqStatList.iterator(); iterator.hasNext(); ) {
                	FreqStat vo = iterator.next();
                	++count;
                	if(count>SHOWNUM){
                		break;
                	}
                    Category c0 = new Category();
             	   if(level.equals("node")){
             		   c0.setLabel(vo.getRecvNodeName());
            	   }else{
            		   c0.setLabel( vo.getRecvNodeName() + "(" + vo.getRecvSysName() + ")");
            	   }
                    
                    cList.add(c0);
                }
                categories.setCategory(cList);
                chart.setCategories(categories);
               
                List< ChartDataset> dList = new ArrayList <ChartDataset >();
                ChartDataset d1 = new ChartDataset();
                d1.setSeriesName("发送量" );
                d1.setColor("167ed5" );
                List< ChartSet> sets1 = new ArrayList <ChartSet >();
                for(Iterator< FreqStat> iterator = freqStatList.iterator(); iterator.hasNext(); ) {
                	FreqStat vo = iterator.next();
                    ChartSet set = new ChartSet();
                    set.setValue(vo.getRecvStValue());
                    sets1.add(set);
                }
               
                ChartDataset d2 = new ChartDataset();
                d2.setSeriesName("接收量" );
                d2.setColor("ffce54" );
                List< ChartSet> sets2 = new ArrayList <ChartSet >();
                for(Iterator< FreqStat> iterator = freqStatList.iterator(); iterator.hasNext(); ) {
                	FreqStat vo = iterator.next();
                    ChartSet set = new ChartSet();
                    set.setValue(vo.getSendStValue());
                    sets2.add(set);
                }
               
                d1.setSet(sets1);
                d2.setSet(sets2);
                dList.add(d1);
                dList.add(d2);
                chart.setDataset(dList);
               
                msg = ChartXmlUtil.forXML(chart );
           } catch(DaoException e) {
                throw new ServiceException("数据统计失败", e);
           }
           return msg;
     }
     
     /**
      * 
      * @param id 节点或系统id
      * @param time 查询的时间条件
      * @param timeType 年 或月
      * @param level 节点 或系统
      * @return
      * @throws ServiceException
      */
     public List<FreqStat> getFreqStatList(String id, String time, String timeType, String level)throws ServiceException {
    	 try {
    		 List< FreqStat> freqStatList = null;
    		 if(level.equals("node")){
    			 freqStatList = dao.getNodeFreqStat(id, time, timeType);
    		 }else{
    			 freqStatList = dao.getSysFreqStat(id, time, timeType);
    		 }
    		 return freqStatList;
    	 } catch(DaoException e) {
    		 throw new ServiceException("数据统计失败", e);
    	 }
     }
    
}
